package cn.lf.android.lawfirm.undertake.approval.controler;


import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.downloadcontroller.modle.Agreement;
import cn.lf.android.lawfirm.undertake.approval.modle.Summary;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.JsonBeans;
import cn.lf.android.lawfirm.utils.SPUtil;

/**
 * 获取承办审批概要
 */
public class SummaryConroller extends BaseController {
    SummaryCallback mSummaryCallback;
    ApprovalCallback mApprovalCallback;
    private Context context;

    public SummaryConroller(SummaryCallback callback, ApprovalCallback callback2, Context context) {
        this.mSummaryCallback = callback;
        this.mApprovalCallback = callback2;
        this.context = context;
    }

    /**
     * @param appId
     */
    public void onSummary(String appId) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("appId", appId);
        params.put("Authorization", spUtil.getAccessToken());
        DebugLog.e("caseId=666=" + appId);
        DebugLog.e("Authorization=666=" + spUtil.getAccessToken());
        new MyOkHttpClient().doGet(AppConfig.LSCaseApproveRecordList_undertake_summary, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("承办审批总结==" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        String arraylist;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                responsObject = myJsonObject.getJSONObject("responsObject");
                                Summary bean = JSON.parseObject(responsObject.toString(), Summary.class);
                                arraylist = responsObject.getJSONArray("attachments")
                                        .toString();
                                List<Agreement> beans = JsonBeans
                                        .getJsonList(
                                                arraylist,
                                                new TypeToken<List<Agreement>>() {
                                                });
                                bean.attachments = beans;
                                DebugLog.e("666==" + beans);
                                mSummaryCallback.onSummaryuccessed(bean);
                            } else {
                                mSummaryCallback
                                        .onSummaryFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        mSummaryCallback.onSummaryFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    /**
     * @param approveopinion 审批备注（长度500，非必填）
     * @param approvesid     申请记录编号（必填）
     * @param submittype     0保存,1同意,2退回（必填）F
     */
    public void onApproval(String approveopinion, String approvesid, String submittype) {
        showProgressDia(context);
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("approveopinion", approveopinion);
        params.put("approvesid", approvesid);
        params.put("submittype", submittype);
        DebugLog.e("approveopinion9==" + approveopinion);
        DebugLog.e("approvesid9==" + approvesid);
        DebugLog.e("submittype9==" + submittype);
        new MyOkHttpClient().doPost(AppConfig.LSCaseApproveRecordList_undertake_Approve, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("承办审批操作9==" + response);
                        JSONObject myJsonObject;
                        JSONObject responsObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                mApprovalCallback.onApprovalsccessed("");
                            } else {
                                mApprovalCallback
                                        .onApprovalFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        dismissProgressDia();
                        mApprovalCallback.onApprovalFailed();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    /**
     * 承办审批列表回调接口
     */
    public interface SummaryCallback {
        /**
         * 承办审批列表请求成功
         */
        public void onSummaryuccessed(Summary beans);

        /**
         * 承办审批列表请求失败
         */
        public void onSummaryFailed();

    }

    /**
     * 审批
     */
    public interface ApprovalCallback {
        /**
         * 审批请求F成功
         */
        public void onApprovalsccessed(String beans);

        /**
         * 审批请求失败
         */
        public void onApprovalFailed();

    }
}
